package com.huizhonglm.diap.widget.expandTab;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class MenuListDouble extends LinearLayout implements MenuBaseAction {
	
	private ListView groupsListView;
	private ListView childrenListView;
	private ArrayList<MenuInfo> groups = new ArrayList<>();
	private LinkedList<MenuInfo> childrenItem = new LinkedList<>();
	private SparseArray<LinkedList<MenuInfo>> children = new SparseArray<>();
	private MenuListAdapter childrenAdapter;
	private MenuListAdapter groupsAdapter;
	private OnSelectListener mOnSelectListener;
	private int tEaraPosition = 0;
	private int tBlockPosition = 0;
	private String showString = "不限";

	public MenuListDouble(Context context, List<MenuInfo> groups) {
		super(context);
		init(context, groups);
	}

	public MenuListDouble(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, null);
	}

//	public void updateShowText(String showArea, String showBlock) {
//		if (showArea == null || showBlock == null) {
//			return;
//		}
//		for (int i = 0; i < groups.size(); i++) {
//			if (groups.get(i).equals(showArea)) {
//				groupsAdapter.setSelectedPosition(i);
//				childrenItem.clear();
//				if (i < children.size()) {
//					childrenItem.addAll(children.get(i));
//				}
//				tEaraPosition = i;
//				break;
//			}
//		}
//		for (int j = 0; j < childrenItem.size(); j++) {
//			if (childrenItem.get(j).replace("不限", "").equals(showBlock.trim())) {
//				childrenAdapter.setSelectedPosition(j);
//				tBlockPosition = j;
//				break;
//			}
//		}
//		setDefaultSelect();
//	}

	private void init(Context context, List<MenuInfo> groupList) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.widget_extab_list_double, this, true);
		groupsListView = (ListView) findViewById(R.id.extab_groups_list);
		childrenListView = (ListView) findViewById(R.id.extab_children_list);
		setBackgroundResource(R.drawable.bg_dd_menu);

		if (!EmptyUtil.isEmpty(groupList)) {
			for (int i =0; i< groupList.size(); i++) {
				MenuInfo info = groupList.get(i);
				groups.add(info);
				LinkedList<MenuInfo> tItem = new LinkedList<>();
				for (MenuInfo  child : info.getChildren()) {
					tItem.add(child);
				}
				children.put(i, tItem);
			}
		}

		groupsAdapter = new MenuListAdapter(context, groups,
				R.drawable.bg_menu_list_double_left_s, R.drawable.bg_menu_list_double_left_n);
		groupsAdapter.setTextSize(17);
		groupsAdapter.setSelectedPositionNoNotify(tEaraPosition);
		groupsListView.setAdapter(groupsAdapter);
		groupsAdapter.setOnItemClickListener(new MenuListAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, int position) {
						if (position < children.size()) {
							childrenItem.clear();
							childrenItem.addAll(children.get(position));
							childrenAdapter.notifyDataSetChanged();
						}
					}
				});
		if (tEaraPosition < children.size())
			childrenItem.addAll(children.get(tEaraPosition));
		childrenAdapter = new MenuListAdapter(context, childrenItem,
				R.drawable.bg_menu_list_double_right_s, R.drawable.bg_white_line_bottom);
		childrenAdapter.setTextSize(15);
		childrenAdapter.setSelectedPositionNoNotify(tBlockPosition);
		childrenListView.setAdapter(childrenAdapter);
		childrenAdapter.setOnItemClickListener(new MenuListAdapter.OnItemClickListener() {

					@Override
					public void onItemClick(View view, final int position) {
						showString = childrenItem.get(position).getName();
						if (mOnSelectListener != null) {
							mOnSelectListener.getValue(childrenItem.get(position));
						}
					}
				});
		if (tBlockPosition < childrenItem.size())
			showString = childrenItem.get(tBlockPosition).getName();
		if (showString.contains("不限")) {
			showString = showString.replace("不限", "");
		}
		setDefaultSelect();

	}

	public void setDefaultSelect() {
		groupsListView.setSelection(tEaraPosition);
		childrenListView.setSelection(tBlockPosition);
	}

	public String getShowText() {
		return showString;
	}

	public void setOnSelectListener(OnSelectListener onSelectListener) {
		mOnSelectListener = onSelectListener;
	}

	public interface OnSelectListener {
		void getValue(MenuInfo info);
	}

	@Override
	public void hide() {
	}

	@Override
	public void show() {
	}
}
